/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import data.DataProvider;
import entities.Artist;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author bertram
 */
public class kidkunstner extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            File file = new File("/home/bertram/regin-wsdls/GlobalKunstnerTabel.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            System.out.println("Root element " + doc.getDocumentElement().getNodeName());
            NodeList nodeLst = doc.getElementsByTagName("row");
            System.out.println("All artists: " + nodeLst.getLength());
            DataProvider prov = new DataProvider();
            for (int s = 0; s < nodeLst.getLength(); s++) {
                
                Node node = nodeLst.item(s);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) node;
                    NodeList nl = eElement.getElementsByTagName("cell");
                    Element id = (Element) nl.item(0);
                    int aId = Integer.parseInt(id.getFirstChild().getNodeValue());
                    Element navn = (Element) nl.item(4);
                    String reginName = navn.getFirstChild().getNodeValue();
                    if (!reginName.isEmpty() && !reginName.contentEquals(" ")) {
                        Artist a = new Artist();
                        a.setReginID(aId);
                        if (reginName.contains("global:")) {
                            reginName = reginName.replaceAll("global: ", "");
                        }
                        if (reginName.contains(",")) {
                            String[] splitName = reginName.split(",", 2);
                            a.setFirstName(splitName[1].trim());
                            a.setLastName(splitName[0].trim());
                        } else if (reginName.contains(" ")) {
                            String[] splitName = reginName.split(" ", 2);
                            a.setFirstName(splitName[0].trim());
                            a.setLastName(splitName[1].trim());
                        } else {
                            a.setFirstName(reginName);
                        }
                        //System.out.println("id: " + aId + a.getName());
                        prov.saveArtist(a);
                    }

                }
                    System.out.println("DONE!");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>

    private String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }
}
